home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / MPW C ƒ / C Include ƒ / Source Files / dictionaries.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-24  |  5.3 KB  |  265 lines  |  [TEXT/MPS ]

  1.  
  2. #include "CIncludesCode.h"
  3. #include <Memory.h>
  4. #include <StdIO.h>
  5.  
  6.  
  7. extern long            totalWords;
  8. extern long            numFiles;
  9. extern ptrArray        *argvPtr;
  10. extern strArray     **filesHdl;
  11. extern Handle        dictionary[numDictionaries];
  12. extern Handle        dependencies;
  13.  
  14.  
  15.  
  16. short dictionaryIndex( char ch )
  17. {
  18.     if ( ch == '_' )
  19.         return 0;
  20.     else
  21.         return ( (ch & 0xDF) - '@' );
  22. }
  23.  
  24.  
  25. void insertWord( short index, char *s )
  26. {
  27.     Handle    dictHdl;
  28.     long    curSize;
  29.     long    endOffset;
  30.     char    *p;
  31.     short    len = strlen( s );
  32.     
  33.     if ( len == 0 )  return;
  34.  
  35.     totalWords++;
  36.     
  37.     dictHdl = dictionary[ dictionaryIndex( *s ) ];
  38.     curSize = GetHandleSize( dictHdl );
  39.     endOffset = *((long*) (*dictHdl));
  40.     
  41.     if ( ( 4 + endOffset + len + 1 ) > curSize )
  42.     {
  43.         SetHandleSize( dictHdl, curSize + 1024 );
  44. //        if ( GetHandleSize( dictHdl ) != (curSize + 1024) )
  45.         if ( MemError() )
  46.         {
  47.             fprintf( stderr, "MemError = %d\n", MemError() );
  48.             errorExit ( "Could not resize dictionary handle!" );
  49.         }
  50.     }
  51.  
  52.     p = *dictHdl + endOffset;
  53.     putShort( p, index );
  54.     strcpy( (p + 2), s );
  55.     
  56.     *((long*) (*dictHdl)) += (3 + len);
  57. }
  58.  
  59.  
  60. #define W(s)  insertWord( -1, s )
  61.  
  62. void fillReservedWords()
  63. {
  64.     W("asm");            W("double");        W("int");            W("sizeof");
  65.     W("auto");            W("else");            W("long");            W("static");
  66.     W("break");            W("entry");            W("new");            W("struct");
  67.     W("case");            W("enum");            W("operator");        W("switch");
  68.     W("catch");            W("extended");        W("overload");        W("template");
  69.     W("char");            W("extern");        W("pascal");        W("this");
  70.     W("class");            W("float");            W("private");        W("typedef");
  71.     W("comp");            W("for");            W("protected");        W("union");
  72.     W("const");            W("friend");        W("public");        W("unsigned");
  73.     W("continue");        W("goto");            W("register");        W("virtual");
  74.     W("default");        W("if");            W("return");        W("void");
  75.     W("delete");        W("inherited");        W("short");            W("volatile");
  76.     W("do");            W("inline");        W("signed");        W("while");
  77. }
  78.  
  79.  
  80. void initDictionaries()
  81. {
  82.     short i;
  83.     
  84.     fprintf( stderr, "Initializing dictionaries...\n" );    
  85.  
  86.     for ( i = 0; i < numDictionaries; ++i )
  87.     {
  88.         dictionary[i] = NewHandle( 4 );
  89.         *((long*) (*dictionary[i])) = 4;   /* "End Pointer" */
  90.     }
  91.  
  92.     fillReservedWords();
  93. }
  94.  
  95.  
  96. void DisposDictionaries()
  97. {
  98.     short i;
  99.     
  100.     for ( i = 0; i < numDictionaries; ++i )
  101.         DisposHandle( dictionary[i] );
  102. }
  103.  
  104.  
  105. char *extractIdentifier( char *dest, char *src)
  106. {
  107.     char    *destPtr = dest;
  108.     short    count;
  109.     
  110.     for ( count = 0; validChar( *src ) && (count < 64); ++count )
  111.         *dest++ = *src++;
  112.     if ( *src == '.' )       // return NULL string for structure members, etc.
  113.     {
  114.         while ( (*src == '.') || validChar( *src ) )
  115.             src++;
  116.         *destPtr = '\0';
  117.     }
  118.     else
  119.         *dest = '\0';
  120.  
  121.     return src;
  122. }
  123.  
  124.  
  125. long nextIdentifier( char *s, Handle dataHdl, long offset, long totalSize )
  126. { // parse text looking for valid identifiers
  127.  
  128.     char *base  = StripAddress(*dataHdl);
  129.     char *p     = base + offset;
  130.     char *q     = base + totalSize;
  131.     char ch;
  132.  
  133.     while ( p < q )
  134.     {
  135.         ch = *p++;
  136.  
  137.         if ( ch == '"' )                                      // skip string literals
  138.             while ( (*p++ != '"') || (*(p-2) == '\\') ) ;
  139.             
  140.         else if ( ch == '#' )                                 // skip pre-processor commands
  141.             while ( validChar(*p++) ) ;
  142.             
  143.         else if ( (ch == '/') && (*p == '/') )                 // skip comments
  144.             while ( *p++ != '\n' ) ;
  145.         
  146.         else if ( (ch == '/') && (*p == '*') )                 // skip comments
  147.             while ( (*p++ != '/') || (*(p-2) != '*') ) ;
  148.         
  149.         else if ( validStart( ch ) )
  150.         {
  151.             p = extractIdentifier( s, p - 1 );
  152.             if ( *s )
  153.                 return ( (long) ( p - base ) );
  154.         }
  155.     }
  156.     return totalSize;
  157. }
  158.  
  159.  
  160.  
  161. long searchDict( char *s, Handle dictHdl )  // returns offset or 0
  162. {
  163.     char *base  = StripAddress(*dictHdl);
  164.     char *p     = base + 6;
  165.     char *limit    = base + *((long*) base);
  166.  
  167.     if ( *s )
  168.         while ( p < limit )
  169.             if ( strcmp( p, s ) == 0 )
  170.                 return ( (long) (p - base - 2 ) );
  171.             else
  172.                 p += strlen( p ) + 3;
  173.     return 0;
  174. }
  175.  
  176.  
  177. long parseFile( Handle dataHdl, short fileIndex )
  178. {
  179.     short    oldFile;
  180.     long     offset;
  181.     long    count = 0;
  182.     long    pos   = 0;
  183.     long    totalSize = GetHandleSize( dataHdl ) - 1;
  184.     Handle    dictHdl;
  185.     char     s[64];
  186.  
  187.     while ( (pos = nextIdentifier( s, dataHdl, pos, totalSize )) < totalSize )
  188.     {
  189.         dictHdl = dictionary[dictionaryIndex( *s )];
  190.  
  191.         if ( offset = searchDict( s, dictHdl ) )
  192.         {
  193.             oldFile = getShort( *dictHdl + offset );
  194.             
  195.             if ( (oldFile != -1) &&
  196.                     (oldFile != fileIndex) &&
  197.                         isDependent( *dependencies, oldFile, fileIndex ) )
  198.                 putShort( (*dictHdl + offset), fileIndex );
  199.         }
  200.         else
  201.         {
  202.             count++;
  203.             insertWord( fileIndex, s );
  204.         }
  205.     }
  206.     return count;
  207. }
  208.  
  209.  
  210. void fillDictionaries()
  211. {
  212.     short    i;
  213.     short    maxLength = maxFilename();
  214.     long    count = 0;
  215.     Handle     dataHdl;
  216.     
  217.     fprintf( stderr, "\nParsing Files:\n" );
  218.  
  219.     for ( i = 0; i < numFiles; ++i )
  220.     {
  221.         fprintf( stderr, "    %-*s", maxLength, (**filesHdl)[i] );
  222.  
  223.         dataHdl = loadDataFile( (*argvPtr)[i + 2] );
  224.         count = parseFile( dataHdl, i );
  225.         DisposHandle( dataHdl );
  226.         
  227.         fprintf( stderr, "  %4d new\n", count);
  228.     }
  229.     
  230.     fprintf( stderr, "\nTotal Entries = %ld\n", totalWords );
  231. }
  232.  
  233.  
  234. void writeDictionary( Handle dictHdl )
  235. {
  236.     long pos = 4;
  237.     long endOffset = *((long*) (*dictHdl));
  238.     char s[64];
  239.     
  240.     while ( pos < endOffset )
  241.     {
  242.         fprintf( stderr, "%3d  ", getShort( *dictHdl + pos ) );
  243.         pos += 2;
  244.         strcpy( s, *dictHdl + pos);
  245.         fprintf( stderr, "%s\n", s );
  246.         pos += strlen( s ) + 1;
  247.     }
  248. }
  249.  
  250.  
  251. void writeAllDictionaries()
  252. {
  253.     short i;
  254.     
  255.     for ( i = 0; i < numDictionaries; ++i )
  256.         writeDictionary( dictionary[i] );
  257. }
  258.  
  259.  
  260. void writeSpecificDirectory( char ch )
  261. {
  262.     writeDictionary( dictionary[dictionaryIndex( ch )] );
  263. }
  264.  
  265.